package com.example.diagnodroid.mouvement;

import android.app.Activity;
import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.widget.TextView;

import com.example.diagnodroid.R;

public class RotationActivity extends Activity implements SensorEventListener {
	private GLSurfaceView mGLSurfaceView;
	private SensorManager mSensorManager;
	private CustomRenderer mRenderer;
	private Sensor mRotationVectorSensor;

	private TextView name;
	private TextView type;
	private TextView precision;
	private TextView raffraichissement;
	private TextView x;
	private TextView y;
	private TextView z;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);

		// find the rotation-vector sensor
		mRotationVectorSensor = mSensorManager
				.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);

		setContentView(R.layout.gyroscope_layout);
		
		name = (TextView) findViewById(R.id.resName);
		type = (TextView) findViewById(R.id.resType);
		precision = (TextView) findViewById(R.id.resPrecision);
		raffraichissement = (TextView) findViewById(R.id.resRaffraichissement);
		x = (TextView) findViewById(R.id.resX);
		y = (TextView) findViewById(R.id.resY);
		z = (TextView) findViewById(R.id.resZ);
		
		mRenderer = new CustomRenderer();
		mGLSurfaceView = (GLSurfaceView) findViewById(R.id.idGLSurfaceView);
		mGLSurfaceView.setRenderer(mRenderer);
		
	}

	@Override
	protected void onResume() {
		// Ideally a game should implement onResume() and onPause()
		// to take appropriate action when the activity looses focus
		super.onResume();
		mSensorManager.registerListener(this, mRotationVectorSensor, 10000);
		mGLSurfaceView.onResume();
	}

	@Override
	protected void onPause() {
		// Ideally a game should implement onResume() and onPause()
		// to take appropriate action when the activity looses focus
		super.onPause();
		mSensorManager.unregisterListener(this);
		mGLSurfaceView.onPause();
	}

	public void onSensorChanged(SensorEvent event) {
		if (event.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR) {
			SensorManager.getRotationMatrixFromVector(mRenderer.getmRotationMatrix(),
					event.values);
			
			name.setText(event.sensor.getName());
			type.setText("Rotation");
			precision.setText(""+event.sensor.getResolution());
			raffraichissement.setText(""+event.sensor.getMinDelay());
			x.setText(""+event.values[0]);
			y.setText(""+event.values[1]);
			z.setText(""+event.values[2]);
		}
	}

	@Override
	public void onAccuracyChanged(Sensor sensor, int accuracy) {
		
	}
}
